/**
 * 
 */
package com.et114.modules.org.ldap;

import java.util.List;
import java.util.Set;
import java.util.Iterator;

//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;


import com.et114.components.acegi.cache.CacheProvider;
import com.et114.components.acegi.ldap.LdapOrgManager;
import com.et114.core.exception.CheckException;
import com.et114.core.utility.ArgumentTokenizer;
import com.et114.modules.org.vo.FunctionVo;
import com.et114.modules.org.vo.RoleVo;
import com.et114.modules.org.vo.TitleMsgVo;
import com.et114.modules.org.vo.UserVo;

/*
 * @author guanhw
 */
public class LdapCacheProvider implements CacheProvider {
//	private Log logger = LogFactory.getLog( this.getClass( ) );
	private  String searchAttr = "cn" ; 
	private LdapOrgManager  ldapOrgManager ; 
	
	public void setLdapOrgManager ( LdapOrgManager ldapOrgManager ) {
		this.ldapOrgManager = ldapOrgManager;
	}

	/* (non-Javadoc)
	 * @see com.et114.components.acegi.cache.CacheProvider#getFunctions()
	 */
	public List getResourcesWithPrincipals ( ) throws CheckException {
		// TODO Auto-generated method stub
		List funcs = ldapOrgManager.getFunctions( "(objectclass=groupOfUniqueNames)" , searchAttr ) ;
		for ( int i = 0 ; i < funcs.size() ; i ++  ) {
			FunctionVo func = ( FunctionVo ) funcs.get( i ) ; 
			String url = func.getUrl() ; 
			Set set = ldapOrgManager.searchForSingleAttributeValues( LdapOrgManager.functionSearchBase , url , "" , "(cn={1})" , "uniqueMember" ) ;
			Iterator iter = set.iterator();
			String value = null;
			while ( iter.hasNext() ) {
				String attr = ( String ) iter.next();
				if ( attr != null ) {
					ArgumentTokenizer at = new ArgumentTokenizer( attr , ',' ) ; 
					while ( at.hasNext() ) {
						String fe = at.next();
						ArgumentTokenizer ats = new ArgumentTokenizer( fe , '=' ) ;
						while( ats.hasNext() ) {
							ats.next();
							value = ats.next();
							break;
						}
						break;
					}
					if ( value != null && attr.indexOf( LdapOrgManager.userSearchBase ) > 0 ) {
						// user;
						UserVo user = new UserVo();
						user.setLoginName( value ) ; 
						func.getUsers().add( user ) ; 
					}
					if ( value != null &&  attr.indexOf( LdapOrgManager.roleSearchBase ) > 0 ) {
						// role
						RoleVo role = new RoleVo();
						role.setRoleName( value ) ; 
						func.getRoles().add( role ) ;
					}
					if ( value != null &&  attr.indexOf( LdapOrgManager.titleSearchBase ) > 0 ) {
						// title
						TitleMsgVo title = new TitleMsgVo();
						title.setTitleName( value ) ;
						func.getTitles().add( title ) ;
					}					
				}

			}
		} 
		return funcs;
	}

	/* (non-Javadoc)
	 * @see com.et114.components.acegi.cache.CacheProvider#getRoles()
	 */
	public List getUsersWithPrincipals ( ) throws CheckException {
		// TODO Auto-generated method stub
		/*
		List users = ldapOrgManager.getUsers( "(objectclass=inetOrgPerson)" , "uid" ) ; 
		for ( int i = 0 ; i < users.size() ; i ++ ) {
			UserVo user = ( UserVo ) users.get( i ) ;
			String username = user.getLoginName();
			user.setPassword( "111111"  );
			user.setWorkFlg ( new Boolean( true ) );
			//init Role
			List roles = ldapOrgManager.getRolesByname( LdapOrgManager.roleSearchBase , username , "uid=" + username + ",ou=people,dc=mycompany,dc=com" , "(uniqueMember={0})" , searchAttr ) ;
			user.getRoles().addAll( roles ) ; 
			//role关联岗位
			
			//init Title 用户直接关联岗位
			List titles = ldapOrgManager.getTitlesByname( LdapOrgManager.titleSearchBase , username , "uid=" + username + ",ou=people,dc=mycompany,dc=com" , "(uniqueMember={0})" , searchAttr ) ;
			for ( int j = 0 ; j < titles.size() ;  j ++ ) {
				TitleMsgVo title = ( TitleMsgVo ) titles.get( j ) ; 
				user.setTitle( title ) ;
				String titlename = title.getTitleName();
				roles = ldapOrgManager.getRolesByname( LdapOrgManager.roleSearchBase , titlename , "cn=" + titlename + ",ou=title,dc=mycompany,dc=com" , "(uniqueMember={0})" , searchAttr  ) ;
				user.getRoles().addAll( roles ) ; 
				logger.info( "" ) ;
			}
		}
		return users;
		*/
		return null ;
	}

}
